<template>
  <div class="container">
    <h1>欢迎来到主页</h1>
    <div class="table-container">
      <table>
        <thead>
        <tr>
          <th>用户ID</th>
          <th>用户名</th>
          <th>身份</th>
        </tr>
        </thead>
        <tbody id="userTableBody">
        <tr v-for="user in users" :key="user.uid">
          <td>{{ user.uid }}</td>
          <td>{{ user.username }}</td>
          <td>{{ user.permission == 1 ?'用户' : '管理员' }}</td>
        </tr>
        </tbody>
      </table>
    </div>
    <div class="logout-container">
      <router-link to="/" class="logout-link">退出登录</router-link>
    </div>
  </div>

</template>

<script>
import axios, {all} from "axios";

export default {
  name: "Home",
  data() {
    return {
      users: [
          {uid: '', username: '', permission: ''}
      ]
    }
  },
  methods: {
    all(){
      axios.get("http://localhost:8080/user/all").then(res => {
        this.users = res.data.data
        console.log(this.users)
      }).catch(err => {
        console.log(err)
      })
    }
  },
  created() {
    this.all()
  }
}
</script>

<style scoped>
.container {
  background: white;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  padding: 30px;
  width: 100%;
  max-width: 800px;
}

h1 {
  color: #2c3e50;
  text-align: center;
  margin-bottom: 30px;
  font-size: 2.5em;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.table-container {
  overflow-x: auto;
  margin: 20px 0;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

table {
  width: 100%;
  border-collapse: collapse;
  background: white;
}

th {
  background: #3498db;
  color: white;
  padding: 15px;
  text-align: left;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 1px;
}

td {
  padding: 12px 15px;
  border-bottom: 1px solid #ddd;
  transition: background-color 0.3s ease;
}

tr:hover td {
  background-color: #f5f5f5;
}

tr:last-child td {
  border-bottom: none;
}

.logout-container {
  position: absolute;
  top: 20px;
  right: 20px;
}

.logout-link {
  display: inline-block;
  padding: 10px 20px;
  background: #e74c3c;
  color: white;
  text-decoration: none;
  border-radius: 5px;
  transition: background-color 0.3s ease;
}

.logout-link:hover {
  background: #c0392b;
}

.loading {
  text-align: center;
  padding: 20px;
  color: #666;
}

.error {
  color: #e74c3c;
  text-align: center;
  padding: 20px;
  background: #fdf2f2;
  border-radius: 5px;
  margin: 10px 0;
}

.delete-btn {
  background-color: #e74c3c;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.delete-btn:hover {
  background-color: #c0392b;
}

.role {
  font-weight: bold;
}

.role.admin {
  color: #e74c3c;
}

.role.user {
  color: #3498db;
}

@media (max-width: 600px) {
  .container {
    padding: 15px;
  }

  h1 {
    font-size: 1.8em;
  }

  th, td {
    padding: 8px;
  }
}

</style>